#__docformat__ = "restructuredtext en"
# ******NOTICE***************
# out.py module by Shoibal Chakravarty
#
# You may copy and use this module as you see fit with no
# guarantee implied provided you keep this notice in all copies.
# *****END NOTICE************
"""
This module contains functions that  print the results file. 
"""

__author__ = "Shoibal Chakravarty (shoibalc@princeton.edu)"
__version__ = "0.1"
__date__ = "2011/August"
__license__ = "BSD"
from  datetime import  datetime

def printresults(fout, Data):
    """
    Function printresults prints some of the results contained in container *Data* to file pointer *fout*.
    """
    print "\nWriting results to file %s\n" % fout.name
    printheader(fout,Data)
    printcomparison(fout,Data,'COST OF GREEN ENERGY','cg')
    printcomparison(fout,Data,'ANNUAL GLOBAL NEW SUBSIDY STARTING THIS PERIOD','c1')
    printcomparison(fout,Data,'ANNUAL GLOBAL TOTAL SUBSIDY','C1')
    printcomparison(fout,Data,'GLOBAL NEW GREEN ENERGY CAPACITY IN CURRENT PERIOD','g')
    printcomparison(fout,Data,'GLOBAL TOTAL GREEN ENERGY','G')
    printcomparison(fout,Data,'GLOBAL TOTAL GREEN SHARE','GS')
    printcomparison(fout,Data,'ANNUAL GLOBAL UTILITY','u1')
    printtable(fout,Data)



def printheader(fout, Data):
    """
    Function printheader prints the initial data to the file pointer *fout*.
    """
    m = datetime.now()
    print >>fout, 'Date: %s' % m.strftime("%H:%M %d-%b-%Y")
    print >>fout, "####  Common Data ####\n"
    print >>fout, 'Length of 1 period (years), %2d' % Data[0][0].years
    print >> fout,'Discount rate, %.2f' % Data[0][0].discount
    print >> fout,'Fossil fuel based electricity costs:'
    print >> fout, "Period, %s (New Fossil), %s (New Fossil), %s (New Fossil), %s (New Fossil), %s (Old Fossil), %s (Old Fossil), %s (Old Fossil), %s (Old Fossil)"\
            % (Data[0][1].name, Data[0][2].name, Data[0][3].name, Data[0][4].name, Data[0][1].name, Data[0][2].name, Data[0][3].name, Data[0][4].name)
    for t in range(Data[0][0].T):
        print >>fout,'{0:2d},{1:.2f},{2:.2f},{3:.2f},{4:.2f},{5:.2f},{6:.2f},{7:.2f},{8:.2f}'.format(\
                t+1, Data[0][1].cn[t], Data[0][2].cn[t], Data[0][3].cn[t], Data[0][4].cn[t], Data[0][1].co[t], Data[0][2].co[t], Data[0][3].co[t], Data[0][4].co[t])

    print >>fout,  'Initial cost of green electricity:, {0:4f} '.format(Data[0][0].cg0)
    print >>fout,  'Learning rate (The learning rate in the last regime is assumed to continue for ever)'
    print >>fout, 'Regime, Number of doublings, Learning rate'
    t=0
    for i in Data[0][0].greenpar:
        t+=1
        print >>fout, '%2d, %3f, %3f' % (t,i[0],i[1])
    print >>fout, '\nBenefit Function: B*log(g+1)'
    print >>fout, "\nPeriod, %s, %s , %s, %s"\
            % (Data[0][1].name, Data[0][2].name, Data[0][3].name, Data[0][4].name)
    for t in range(Data[0][0].T):
        print >>fout,'{0:2d},{1:.2f},{2:.2f},{3:.2f},{4:.2f}'.format(t+1, Data[0][1].B[t], Data[0][2].B[t], Data[0][3].B[t], Data[0][4].B[t])
    print >>fout, '\nNOTE: Utility and Subsidy below are the undiscounted numbers for the given Period\n'


def printtable(fout,Data):
    """
    Function printtable prints the results for each region of a  scenario in the container *Data* to the file pointer *fout*.
    """
    R = []
    T = Data[0][0].T
    for region in Data[0][1:]:
        R.append(region.name)
    for scen in Data[1:]:
        print >>fout, "\n#################################\n\n",scen.scenario,"\n"
        for region in R:
            reg = scen.get(region)
            print >>fout, '%s,%s'%(region,reg.descriptor)
            print >> fout, "Period,Costof Green Energy,Annual Total Energy,Annual Green Energy,Annual Fossil Energy,,New Energy Demand in this Period,New Green in this Period,\
                    New Fossil in this Period,,Discount Factor,Annual Utility,Annual Total Subsidy,Annual New Subsidy"
            print >>fout, '{0:2d},{1:.3f},{2:.2f},{3:.2f},{4:.2f},,,,,,{5:.2f}'.format(0,scen.get('WORLD').cg[0],reg.E[0],reg.G[0],reg.F[0],Data[0][0].RR[0])
            for t in range(T):
                print >>fout, '{0:2d},{1:.3f},{2:.2f},{3:.2f},{4:.2f},,{5:.2f},{6:.2f},{7:.2f},,{8:.2f},{9:.2f},{10:.2f},{11:.2f}'.format(\
                    t+1,scen.get('WORLD').cg[t+1],reg.E[t+1],reg.G[t+1],reg.F[t+1],reg.e[t],reg.g[t],reg.f[t],Data[0][0].RR[t+1],reg.u1[t],reg.C1[t],reg.c1[t])
            print >>fout,''


def printcomparison(fout, Data,header,classmember):
    """
    Function printcomparison tabulates the results from different scenarios for the results in the vector *classmember* for the global data in the container *Data*.
    """

    print >>fout, "\n#################################\n\n",header,"\n"
    print >>fout, 'Period,',
    for i in Data[1:]:
        print >>fout,'%s,' % i.WORLD.scenario,
    print >>fout,''
    T = Data[0][0].T
    l = len(getattr(Data[1].WORLD,classmember))
    if l == T:
        z=1
    else:
        z=0
    for t in range(l):
        print >>fout, '%2d,' % (t+z),
        for i in Data[1:]:
            print >>fout, '%.2f,'% getattr(i.WORLD,classmember)[t],
        print >>fout,''

